package com.example.basic.web.service.roleVsMenu.impl;

import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.IdUtils;
import com.example.basic.web.mapper.roleVsMenu.RoleVsMenuMapper;
import com.example.basic.web.model.role.Role;
import com.example.basic.web.model.roleVsMenu.RoleVsMenu;
import com.example.basic.web.service.role.RoleService;
import com.example.basic.web.service.roleVsMenu.RoleVsMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class RoleVsMenuServiceImpl extends BaseEntityServiceImpl<RoleVsMenuMapper, RoleVsMenu> implements RoleVsMenuService {

    @Resource
    private RoleVsMenuMapper roleVsMenuMapper;

    @Resource
    private RoleService roleService;

    /**
     * @param sysType
     * @param roleVsMenuList 系统用户组和功能对应关系列表
     * @return
     * @create 系统用户组和功能批量修改功能
     */
    @Override
    public boolean batchModify(String roleCode, String sysType, List<RoleVsMenu> roleVsMenuList) {
        notNull(roleCode,"角色代码不能为空");

        // 核查角色是否存在
        Role role = roleService.detail(roleCode);
        if (role == null) {
            throw new ServiceException("角色信息不存在!");
        }

        //删除已维护的对应关系
        boolean result = roleVsMenuMapper.deleteByRoleCode(roleCode,sysType);

        if(roleVsMenuList.size() > 0){
            for (RoleVsMenu roleVsMenu : roleVsMenuList) {
                notNull(roleVsMenu.getMenuCode(),"菜单代码不能为空");
                roleVsMenu.setSysType(sysType);
                roleVsMenu.setRoleCode(roleCode);
                roleVsMenu.setId(IdUtils.fastUUID());
            }
            //新增用户组与功能对应关系
            return this.saveBatch(roleVsMenuList);
        }

        return result;
    }

    /**
     * @param roleCode 角色代码
     * @param sysType 系统类型
     * @return
     * @create 查询角色对应菜单对应关系列表
     */
    @Override
    public List<RoleVsMenu> queryList(String roleCode, String sysType) {
        notNull(roleCode,"角色代码不能为空");
        notNull(sysType,"系统代码不能为空");

        return roleVsMenuMapper.queryList(roleCode,sysType);
    }

    /**
     * 删除已维护关系
     * @param roleCode 角色编码
     * @return
     */
    @Override
    public boolean deleteByRoleCode(String roleCode,String sysType) {
        notNull(roleCode,"角色编码不能为空");
        return roleVsMenuMapper.deleteByRoleCode(roleCode, sysType);
    }
}
